package com.example.dao;

import com.example.domain.Book;
import com.github.pagehelper.Page;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

public interface BookMapper {


    Integer addBook(Book book);
    /*
        添加图书
     */

    Integer editBook(Book book);
    /*
       更新数据
     */

    @Select("select * from book where book_status != 3 order by book_uploadtime desc ")
    @Results(id = "bookMap", value = {@Result(property = "id", column = "book_id", id = true), @Result(property = "name", column = "book_name"), @Result(property = "isbn", column = "book_isbn"), @Result(property = "press", column = "book_press"), @Result(property = "author", column = "book_author"), @Result(property = "pagination", column = "book_pagination"), @Result(property = "price", column = "book_price"), @Result(property = "uploadTime", column = "book_uploadtime"), @Result(property = "status", column = "book_status"), @Result(property = "borrower", column = "book_borrower"), @Result(property = "borrowTime", column = "book_borrowtime"), @Result(property = "returnTime", column = "book_returntime")})
    Page<Book> selectNewBooks();
    /*
           首页图书推荐
     */


    @Select("select * from Book where book_id = #{id}")
    @ResultMap("bookMap")
    Book findBookById(String id);
    /*
        图书信息查询
    */


    @Select("      <script>\n" + "            select * from book\n" + "            <where>\n" + "                <if test=\"name!=null and name.trim()!='' \">\n" + "                    and book_name like concat('%',#{name},'%')\n" + "                </if>\n" + "                <if test=\"author!=null and author.trim()!='' \">\n" + "                    and book_author like concat('%',#{author},'%')\n" + "                </if>\n" + "                <if test=\"press!=null and press.trim()!='' \">\n" + "                    and book_press like concat('%',#{press},'%')\n" + "                </if>\n" + "            </where>\n" + "            order by book_status\n" + "        </script>")
    @ResultMap("bookMap")
    Page<Book> searchBooks(Book book);
    /*
        注解式动态SQL
            图书查找
     */


    // 查询所有未归还
    @Select("        <script>\n" + "            select * from book\n" + "            where book_borrower =#{borrower}\n" + "            and book_status = 1\n" + "            <if test=\"name!=null and name.trim()!='' \">\n" + "                and book_name like concat('%',#{name},'%')\n" + "            </if>\n" + "            <if test=\"author!=null and author.trim()!='' \">\n" + "                and book_author like concat('%',#{author},'%')\n" + "            </if>\n" + "            <if test=\"press!=null and press.trim()!='' \">\n" + "                and book_press like concat('%',#{press},'%')\n" + "            </if>\n" + "\n" + "            or book_status = 2\n" + "            <if test=\"name!=null and name.trim()!='' \">\n" + "                and book_name like concat('%',#{name},'%')\n" + "            </if>\n" + "            <if test=\"author!=null and author.trim()!='' \">\n" + "                and book_author like concat('%',#{author},'%')\n" + "            </if>\n" + "            <if test=\"press!=null and press.trim()!='' \">\n" + "                and book_press like concat('%',#{press},'%')\n" + "            </if>\n" + "        </script>")
    @ResultMap("bookMap")
    Page<Book> selectBorrowed(Book book);

    // 查询当前账号未归还
    @Select("        <script>\n" + "            select * from book\n" + "            where book_borrower =#{borrower}\n" + "            and book_status in (1,2)\n" + "            <if test=\"name!=null and name.trim()!='' \">\n" + "                and book_name like concat('%',#{name},'%')\n" + "            </if>\n" + "            <if test=\"author!=null and author.trim()!='' \">\n" + "                and book_author like concat('%',#{author},'%')\n" + "            </if>\n" + "            <if test=\"press!=null and press.trim()!='' \">\n" + "                and book_press like concat('%',#{press},'%')\n" + "            </if>\n" + "        </script>")
    @ResultMap("bookMap")
    Page<Book> selectMyBorrowed(Book book);


}
